//
//  UserData.swift
//  ReadHub
//
//  Created by 龙轶群 on 2017/8/14.
//  Copyright © 2017年 longyiqun. All rights reserved.
//

import Foundation
import AvatarImageView

struct UserData: AvatarImageViewDataSource {
    var name: String
    var avatar: UIImage?
    
    init() {
        
        let userName = UserDefaults.standard.string(forKey: "username")
        
        if( userName != nil) {
            name = userName!
        } else {
            name = ""
            avatar = UIImage(named: "login")
        }
    }
    
    static func findFirstLetterFromString(aString: String) -> String {
        //转变成可变字符串
        let mutableString = NSMutableString.init(string: aString)
        
        //将中文转换成带声调的拼音
        CFStringTransform(mutableString as CFMutableString, nil,      kCFStringTransformToLatin, false)
        
        //去掉声调
        let pinyinString = mutableString.folding(options:          String.CompareOptions.diacriticInsensitive, locale:   NSLocale.current)
        
        //将拼音首字母换成大写
        let strPinYin = polyphoneStringHandle(nameString: aString,    pinyinString: pinyinString).uppercased()
        
        //截取大写首字母
        let firstString = strPinYin.substring(to:     strPinYin.index(strPinYin.startIndex, offsetBy: 1))
        
        //判断首字母是否为大写
        let regexA = "^[A-Z]$"
        let predA = NSPredicate.init(format: "SELF MATCHES %@", regexA)
        return predA.evaluate(with: firstString) ? firstString : "#"
    }
    //多音字处理，根据需要添自行加
    static func polyphoneStringHandle(nameString: String, pinyinString: String) -> String {
        if nameString.hasPrefix("长") {return "chang"}
        if nameString.hasPrefix("沈") {return "shen"}
        if nameString.hasPrefix("厦") {return "xia"}
        if nameString.hasPrefix("地") {return "di"}
        if nameString.hasPrefix("重") {return "chong"}
        return pinyinString
    }

    
}
